<?php
namespace ImiApp\Service;

use Imi\Bean\Annotation\Bean;
use ImiApp\Model\Statistics;

/**
 * @Bean("StatisticsService")
 */
class StatisticsService
{
    /**
     * 根据修改时间获取记录
     *
     * @param int $time
     * @return \ImiApp\Model\Statistics|null
     */
    public function getByModifyTime(int $time): ?Statistics
    {
        return Statistics::find([
            'modify_time'   =>  $time,
        ]);
    }

    /**
     * 使用数据创建记录
     *
     * @param array $data
     * @return \ImiApp\Model\Statistics
     */
    public function createByData(array $data): Statistics
    {
        $record = Statistics::newInstance($data);
        $record->save();
        return $record;
    }

    /**
     * 获取最新记录
     *
     * @return \ImiApp\Model\Statistics|null
     */
    public function getLastModify(): ?Statistics
    {
        return Statistics::query()->order('modify_time', 'desc')->limit(1)->select()->get();
    }

}
